package net.betou.cms.manager.main.impl;

import net.betou.cms.dao.main.CmsUserExtDao;
import net.betou.cms.entity.main.CmsUser;
import net.betou.cms.entity.main.CmsUserExt;
import net.betou.cms.manager.main.CmsUserExtMng;
import net.betou.common.hibernate3.Updater;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class CmsUserExtMngImpl implements CmsUserExtMng{
	public CmsUserExt save( CmsUserExt ext, CmsUser user ) {
		ext.blankToNull( );
		ext.setUser( user );
		dao.save( ext );
		return ext;
	}

	public CmsUserExt update( CmsUserExt ext, CmsUser user ) {
		CmsUserExt entity = dao.findById( user.getId( ) );
		if( entity == null ){
			entity = save( ext, user );
			user.getUserExtSet( ).add( entity );
			return entity;
		}else{
			Updater< CmsUserExt > updater = new Updater< CmsUserExt >( ext );
			updater.include( "gender" );
			updater.include( "birthday" );
			ext = dao.updateByUpdater( updater );
			ext.blankToNull( );
			return ext;
		}
	}

	private CmsUserExtDao dao;

	@Autowired
	public void setDao( CmsUserExtDao dao ) {
		this.dao = dao;
	}
}